package com.lottery.material.controller.portal.user;

import com.lottery.material.controller.base.BaseController;
import com.lottery.material.enumeration.ResultEnum;
import com.lottery.material.jedis.JedisClient;
import com.lottery.material.pojo.article.ArticleInfo;
import com.lottery.material.pojo.portaluser.CommentInfo;
import com.lottery.material.pojo.portaluser.PortalUserInfo;
import com.lottery.material.pojo.portaluser.UserLikeRecord;
import com.lottery.material.resolver.CustomException;
import com.lottery.material.service.portal.article.ArticleInfoService;
import com.lottery.material.service.portal.user.CommentInfoService;
import com.lottery.material.service.portal.user.PortalUserInfoService;
import com.lottery.material.service.portal.user.UserLikeRecordService;
import com.lottery.material.util.CalendarUtil;
import com.lottery.material.util.ExceptionUtil;
import com.lottery.material.util.ParameterUtil;
import com.lottery.material.util.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Description:    点赞/取消点赞Controller
 * Author:         sunny
 * CreateDate:     2019-04-15 09:32
 */
@RequestMapping("/portal/user")
@Slf4j
@Controller
public class PortalUserLikeController extends BaseController {

    @Autowired
    @Qualifier("jedisClientCluster")
    private JedisClient jedisClient;

    @Autowired
    private UserLikeRecordService userLikeRecordService;
    @Autowired
    private ArticleInfoService articleInfoService;
    @Autowired
    private CommentInfoService commentInfoService;
    @Autowired
    private PortalUserInfoService portalUserInfoService;

    /**
     * description: 点赞/取消点赞
     * author sunny
     * createDate 2019-04-15 上午 9:36
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/giveLike", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult giveLike(HttpServletRequest request) {
        Map<String, String> reqParamMap = new HashMap<>();
        //解析参数
        if (ParameterUtil.analyticParameter(request, reqParamMap)) {
            throw new CustomException(ResultEnum.PARSE_EXCEPTION);
        }

        //非空判断及用户登录是否过期验证
        ParameterUtil.checkLogin(reqParamMap, jedisClient, portalUserInfoService);

        ResponseResult respDate = validateEmpty(reqParamMap);
        if (null != respDate) {
            return respDate;
        }

        String isLike = reqParamMap.get("isLike");  //1：取消点赞；2：点赞
        if (StringUtils.isBlank(isLike)) {
            return ResponseResult.buildFormatResult(ResultEnum.PARAMETER_NOT_NULL, "isLike");
        } else if (!"1".equals(isLike) && !"2".equals(isLike)) {
            return ResponseResult.buildFormatResult(ResultEnum.PARAMETER_ERROR, "isLike");
        }
        String likeType = reqParamMap.get("likeType");  //1：文章；2：评论
        if (StringUtils.isBlank(likeType)) {
            return ResponseResult.buildFormatResult(ResultEnum.PARAMETER_NOT_NULL, "likeType");
        } else if (!"1".equals(likeType) && !"2".equals(likeType)) {
            return ResponseResult.buildFormatResult(ResultEnum.PARAMETER_ERROR, "likeType");
        }
        String objectId = reqParamMap.get("objectId");
        if (StringUtils.isBlank(objectId)) {
            return ResponseResult.buildFormatResult(ResultEnum.PARAMETER_NOT_NULL, "objectId");
        }

        //签名验证
        ResponseResult responseResult = ParameterUtil.checkSign(reqParamMap);
        if (null != responseResult) {
            return responseResult;
        }

        try{
            if("1".equals(likeType)){
                ArticleInfo articleInfo = articleInfoService.findByArticleId(objectId);
                if(Objects.isNull(articleInfo)){
                    return ResponseResult.buildResult(ResultEnum.ARTICLE_NOT_EXIST_FAIL);
                }
            }else if("2".equals(likeType)){
                CommentInfo commentInfo = commentInfoService.findByCommentId(objectId);
                if(Objects.isNull(commentInfo)){
                    return ResponseResult.buildResult(ResultEnum.COMMENT_NOT_EXIST_FAIL);
                }
            }

            //封装数据
            UserLikeRecord userLikeRecord = new UserLikeRecord();
            userLikeRecord.setUserId(reqParamMap.get("userId"));
            userLikeRecord.setLikeType(Integer.valueOf(likeType));
            userLikeRecord.setObjectId(objectId);
            if ("1".equals(isLike)) {
                Integer likeCount = userLikeRecordService.deleteUserLikeRecord(userLikeRecord);
                if(-1 == likeCount){
                    return ResponseResult.buildResult(ResultEnum.NO_LIKE_FAIL);
                }

                return ResponseResult.buildResult(ResultEnum.NO_LIKE_SUCCESS);
            } else {
                userLikeRecord.setCreateTime(CalendarUtil.getSystemCurrentDate());
                Integer likeCount = userLikeRecordService.saveUserLikeRecord(userLikeRecord);
                if(-1 == likeCount){
                    return ResponseResult.buildResult(ResultEnum.LIKE_FAIL);
                }
                return ResponseResult.buildResult(ResultEnum.LIKE_SUCCESS);
            }
        } catch (Exception e) {
            log.error("【点赞/取消点赞出现异常】:{}", ExceptionUtil.logExceptionStack(e));
            if ("1".equals(isLike)) {
                throw new CustomException(ResultEnum.NO_LIKE_FAIL);
            } else {
                throw new CustomException(ResultEnum.LIKE_FAIL);
            }
        }
    }
}
